package com.lemon.smartsafety.utils.common;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;

import java.util.Random;

/**
 * Created by Cooliean on 16/2/4.
 */
public class DrawableUtils {

    /**
     * 获取随机颜色
     *
     * @param colors
     * @return
     */
    public static ColorStateList getRandomColorStateList(String[] colors) {
        Random random      = new Random();
        String colorString = colors[random.nextInt(colors.length)];
        return ColorStateList.valueOf(Color.parseColor(colorString));
    }


    /**
     * 获取随机颜色
     *
     * @param colors
     * @return
     */
    public static int getRandomColor(String[] colors) {
        Random random      = new Random();
        String colorString = colors[random.nextInt(colors.length)];
        return Color.parseColor(colorString);
    }

    /**
     * ========================================================
     * tint drawable with Color or ColorStateList
     * using the DrawableCompat in support v4 library
     * ========================================================
     */
    public static Drawable $tint(Drawable originDrawable, int color) {
        return $tint(originDrawable, ColorStateList.valueOf(color));
    }

    public static Drawable $tint(Drawable originDrawable, int color, PorterDuff.Mode tintMode) {
        return $tint(originDrawable, ColorStateList.valueOf(color), tintMode);
    }

    public static Drawable $tint(Drawable originDrawable, ColorStateList colorStateList) {
        return $tint(originDrawable, colorStateList, null);
    }

    public static Drawable $tint(Drawable originDrawable, ColorStateList colorStateList, PorterDuff.Mode tintMode) {
        Drawable tintDrawable = DrawableCompat.wrap(originDrawable);
        if (tintMode != null) {
            DrawableCompat.setTintMode(tintDrawable, tintMode);
        }
        DrawableCompat.setTintList(tintDrawable, colorStateList);
        return tintDrawable;
    }


    public static Drawable $tint(Drawable originDrawable, String[] colors) {
        return $tint(originDrawable, ColorStateList.valueOf(getRandomColor(colors)), null);
    }

}
